import logging, time, datetime
from google.appengine.ext import webapp
from controllers import controller
from config import *
from models import *

from lib import GoogleS3

class Yeah(controller.Controller):
  
  def get(self,timestamp=None,number=None):
    logging.info('YEAH['+str(timestamp)+']['+str(number)+']')
    
    if timestamp is None or len(timestamp) == 0 or int(timestamp) == 0:
      timestamp = int(time.mktime(datetime.datetime.now().timetuple()))
    if number is None or not len(number):
      number = 10
    
    if number > 0:
      results = YeahRecord.all().order('-timestamp').filter('timestamp <',int(timestamp)).fetch(int(number))
    elif number < 0:
      number = number * -1.0
      results = YeahRecord.all().order('-timestamp').filter('timestamp >',int(timestamp)).fetch(int(number))
      
    output = []
    
    generator = GoogleS3.QueryStringAuthGenerator(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY)
    generator.set_expires_in(360)
    
    for result in results:
      record = {}
      #record['url'] = generator.get('yeahyeahyeahyeahyeah', result.s3_key)
      record['url'] = 'http://yeahyeahyeahyeahyeah.s3.amazonaws.com/'+result.s3_key
      record['timestamp'] = int(time.mktime(result.created.timetuple()))
      output.append(record)
    
    return self.json(output)